package com.aviccxzx.smarthome.UserModule;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.aviccxzx.smarthome.ConfigModule.GosAirlinkChooseDeviceWorkWiFiActivity;
import com.aviccxzx.smarthome.ConfigModule.GosDeviceReadyActivity;
import com.aviccxzx.smarthome.DeviceModule.GosDeviceModuleBaseActivity;
import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.base.GosUserModuleBaseActivity;
import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.gizwits.gizwifisdk.enumration.GizWifiConfigureMode;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.enumration.GizWifiGAgentType;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

/**
 * Created by joey on 2016/10/27.
 */
public class IsLinkingActivity extends GosUserModuleBaseActivity implements View.OnClickListener {
    TextView islinking_cancle;
    ImageView radar_scan_img;
    Animation rotateAnim;
    /** 配置用参数 */
    String workSSID, workSSIDPsw;

    /** The String */
    String timerText;

    List<GizWifiGAgentType> modeList, modeDataList;
    /** 倒计时 */
    int secondleft = 60;
    /** The timer */
    Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.islinking_lay_v19);
        }else {
        }
        ((TextView) findViewById(R.id.title_tv)).setText("添加设备");
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        radar_scan_img = (ImageView)findViewById(R.id.radar_scan_img);
        setRotate();
        initData();
        startAirlink();
    }

    private void initData() {
        workSSID = spf.getString("workSSID", "");
        workSSIDPsw = spf.getString("workSSIDPsw", "");
        modeDataList = new ArrayList<GizWifiGAgentType>();
        modeDataList.add(GizWifiGAgentType.GizGAgentESP);
        modeDataList.add(GizWifiGAgentType.GizGAgentMXCHIP);
        modeDataList.add(GizWifiGAgentType.GizGAgentHF);
        modeDataList.add(GizWifiGAgentType.GizGAgentRTK);
        modeDataList.add(GizWifiGAgentType.GizGAgentWM);
        modeDataList.add(GizWifiGAgentType.GizGAgentQCA);
        modeDataList.add(GizWifiGAgentType.GizGAgentTI);
        modeDataList.add(GizWifiGAgentType.GizGAgentFSK);
        modeDataList.add(GizWifiGAgentType.GizGAgentMXCHIP3);
        modeDataList.add(GizWifiGAgentType.GizGAgentBL);
        modeDataList.add(GizWifiGAgentType.GizGAgentAtmelEE);
        modeDataList.add(GizWifiGAgentType.GizGAgentOther);
        modeList = new ArrayList<GizWifiGAgentType>();

        modeList.add(modeDataList
                .get(GosAirlinkChooseDeviceWorkWiFiActivity.modeNum));

    }

    private void startAirlink() {
        GizWifiSDK.sharedInstance().setDeviceOnboarding(workSSID, workSSIDPsw,
                GizWifiConfigureMode.GizWifiAirLink, null, 60, modeList);
        //        handler.sendEmptyMessage(handler_key.START_TIMER.ordinal());

    }

    private void setRotate() {
        // 加载动画
        rotateAnim = AnimationUtils.loadAnimation(this, R.anim.radar_rotate);
        LinearInterpolator lin = new LinearInterpolator();// 匀速旋转
        rotateAnim.setInterpolator(lin);
        radar_scan_img.startAnimation(rotateAnim);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_title_left_view:
                finish();
                break;
            case R.id.radar_scan_img:
                showToast("点击取消");
                //取消连接
                // 回到上个界面
                finish();
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        rotateAnim.cancel();
    }


    private enum handler_key {

        /**
         * 倒计时提示
         */
        TIMER_TEXT,

        /**
         * 倒计时开始
         */
        START_TIMER,

        /**
         * 配置成功
         */
        SUCCESSFUL,

        /**
         * 配置失败
         */
        FAILED,

    }

    /**
     * The handler.
     */
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            handler_key key =handler_key.values()[msg.what];
            switch (key) {
                case TIMER_TEXT:

                    //                    tvTimer.setText(timerText);
                    break;

                case START_TIMER:
                    //                    isStartTimer();
                    break;

                case SUCCESSFUL:
//                    App.getApp().showToast("成了");
                    GosDeviceModuleBaseActivity.deviceslist = GizWifiSDK.sharedInstance()
                            .getDeviceList();
//                    App.getApp().showToast("成了。。。"+GosDeviceModuleBaseActivity.deviceslist.toString());
                    Log.d("OK",GosDeviceModuleBaseActivity.deviceslist.toString());
                    finish();
                    break;

                case FAILED:
                    Intent intent = new Intent(IsLinkingActivity.this,
                            GosDeviceReadyActivity.class);
                    startActivity(intent);
                    finish();
                    break;

                default:
                    break;
            }
        }

    };

    // 屏蔽掉返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            quitAlert(this, null);
            return true;
        }
        return false;
    }

    // 倒计时
    //    public void isStartTimer() {
    //
    //        secondleft = 60;
    //        timer = new Timer();
    //        timer.schedule(new TimerTask() {
    //            @Override
    //            public void run() {
    //                secondleft--;
    //                rpbConfig.setProgress((60 - secondleft) * (100 / 60.0));
    //                if (secondleft == 58) {
    //                    timerText = (String) getText(R.string.searching_device);
    //                    handler.sendEmptyMessage(handler_key.TIMER_TEXT.ordinal());
    //                } else if (secondleft == 30) {
    //                    timerText = (String) getText(R.string.searched_device);
    //                    handler.sendEmptyMessage(handler_key.TIMER_TEXT.ordinal());
    //                } else if (secondleft == 28) {
    //                    timerText = (String) getText(R.string.trying_join_device);
    //                    handler.sendEmptyMessage(handler_key.TIMER_TEXT.ordinal());
    //                }
    //            }
    //        }, 1000, 1000);
    //    }

    /**
     * 设备配置回调
     *
     * @param result
     *            错误码
     * @param mac
     *            MAC
     * @param did
     *            DID
     * @param productKey
     *            PK
     */
    protected void didSetDeviceOnboarding(GizWifiErrorCode result, String mac,
                                          String did, String productKey) {
        if (GizWifiErrorCode.GIZ_SDK_DEVICE_CONFIG_IS_RUNNING == result) {
            return;
        }

        if (timer != null) {
            timer.cancel();
        }
        Message message = new Message();
        if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
            message.what = handler_key.SUCCESSFUL.ordinal();
        } else {
            message.what = handler_key.FAILED.ordinal();
            message.obj = toastError(result);
        }
        Log.i("Apptest", result.toString());
        handler.sendMessage(message);
    }
}
